package itsm.isperp.module.controller.project;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.data.mybatis.domain.AbstractTreeNode;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.dto.project.ProjectDomain;
import itsm.isperp.module.dto.project.ProjectDomain.ProjectDomainType;
import itsm.isperp.module.entity.configuration.ConfCi;
import itsm.isperp.module.entity.project.ProjProject;
import itsm.isperp.module.entity.project.ProjProjectCategory;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.customer.CusCustomerService;
import itsm.isperp.module.service.incident.IcdIncidentService;
import itsm.isperp.module.service.project.ProjProjectCategoryService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.service.task.TskTaskService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 项目管理控制器
 * 
 * @author lizx
 * @date 2013-2-26
 * @version V1.0
 */
@Controller
@RequestMapping("project/project/library")
public class ProjProjectLibraryController extends BaseController<ProjProject> {

	@Autowired
	protected ProjProjectService projProjectService;

	@Autowired
	protected ProjProjectCategoryService projProjectCategoryService;

	@Autowired
	protected AppDictService appDictService;

	@Autowired
	protected IcdIncidentService IcdIncidentService;

	@Autowired
	protected TskTaskService tskTaskService;
 

	@Override
	public ProjProjectService getService() {
		return this.projProjectService;
	}

	@Override
	public String getPageTitle() {
		return "项目管理";
	}

	/**
	 * 项目库管理
	 * 
	 * @param req
	 *            请求数据
	 * @return 归档界面
	 */
	@RequestMapping("projectLibraryManage")
	public ModelAndView projectLibraryManage(HttpServletRequest req) {
		return this.getModelAndView();
	}

	/**
	 * 项目库列表
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping("libraryList")
	@ResponseBody
	public PageResponse<Map<String, Object>> libraryList(JqGridRequest request,
			HttpServletRequest req) {

		request.addFilterRule("stage", JqGridFilterRuleOp.eq,
				ProjProjectService.STAGE_FINISHED);

		request.setProjectDomain(ProjectDomain.newInstance("id",
				ProjectDomainType.Management));
		return this.projProjectService.findAllDetailByRequest(request).toPage();

	}

	/**
	 * 项目库编辑
	 * 
	 * @param req
	 *            请求数据
	 * @return 项目库编辑界面
	 */
	@RequestMapping("projectLibraryEdit")
	public ModelAndView projectLibraryEdit(String id, HttpServletRequest req) {

		ModelAndView mv = getModelAndView();

		// 项目基本信息
		ProjProject model = null;

		if (StringUtils.isEmpty(id)) {

			model = new ProjProject();
			model.setId(HibernateUUIDGenerator.generate());
		} else {
			model = this.projProjectService.findOne(id);

			if (StringUtils.isNotEmpty(model.getCusCustomerId())) {

				CusCustomerService cusCustomerService = ContextHolder
						.getSpringBean("cusCustomerService");

				mv.addObject("cusCustomerName",
						cusCustomerService.findOne(model.getCusCustomerId())
								.getName());
			}
		}

		if (StringUtils.isNotEmpty(model.getProjCategoryId())) {
			ProjProjectCategory cate = this.projProjectCategoryService
					.findOne(model.getProjCategoryId());
			mv.addObject("projectCategoryName", cate.getTreenodeNamePath());
		}

		mv.addObject("projectLevels",
				appDictService.findByType("PROJECT_LEVEL"));
		mv.addObject("projectTypes", appDictService.findByType("PROJECT_TYPE"));

		mv.addObject("projectStatus",
				appDictService.findByType("PROJECT_STATUS"));
		mv.addObject("model", model);

		mv.addObject("name", "项目归档");

		return mv;
	}

	@RequestMapping("projectCi")
	public ModelAndView projectCi(String projectId) {
		ModelAndView mv = this.getModelAndView();

		mv.addObject("projectId", projectId);
		return mv;
	}

	@RequestMapping("listProjectCi")
	@ResponseBody
	public PageResponse<ConfCi> listProjectCi(String projectId,
			JqGridRequest request) {
		if (StringUtils.isEmpty(projectId)) {
			return null;
		}
		request.addFilterRule("project_id", JqGridFilterRuleOp.eq, projectId);

		return this.getService().findProjectCiByRequest(request).toPage();
	}

	/**
	 * 
	 * @param projectId
	 */
	@RequestMapping("getProjectCiCategoryTree")
	@ResponseBody
	public List<ITreeNode> getProjectCiCategoryTree(String projectId) {

		List<Map<String, Object>> list = this.getService()
				.getLibraryProjectCiCategory(projectId);

		List<ITreeNode> nodes = new ArrayList<>();

		for (Map<String, Object> map : list) {

			ITreeNode tn = new AbstractTreeNode();
			tn.setName(map.get("name").toString() + "("
					+ map.get("count_id").toString() + ")");
			tn.setId(map.get("id").toString());
			tn.setTreenodePath(map.get("id").toString());
			nodes.add(tn);

		}
		return nodes;
	}

	/**
	 * 项目工单统计
	 * 
	 * @param projectId
	 * @return
	 */
	@RequestMapping("projectOrderSum")
	public ModelAndView projectOrderSum(String projectId) {
		ModelAndView mv = this.getModelAndView();

		mv.addObject("projectId", projectId);

		// 构造查询条件
		JqGridRequest req = new JqGridRequest();
		req.setSelectColumns("count(*) as sum");
		req.addFilterRule("project_id", JqGridFilterRuleOp.eq, projectId);

		// 获取事件单总数
		List<Map<String, Object>> indList = IcdIncidentService
				.findAllDetailByRequest(req).getContent();
		if (indList.size() > 0) {
			mv.addObject("incident_sum", indList.get(0).get("sum"));
		} else {
			mv.addObject("incident_sum", 0);
		}

		// 获取问题单总数
		mv.addObject("problem_sum", 0);

		// 获取变更单总数
		mv.addObject("change_sum", 0);

		// 获取发布单单总数
		mv.addObject("release_sum", 0);

		// 获取任务单总数
		List<Map<String, Object>> tskList = tskTaskService
				.findAllDetailByRequest(req).getContent();
		if (tskList.size() > 0) {
			mv.addObject("task_sum", tskList.get(0).get("sum"));
		} else {
			mv.addObject("task_sum", 0);
		}

		mv.addObject("projectId", projectId);

		return mv;
	}

}
